using System;
using System.Collections.Generic;
using Csla;
using Ent.AccesoDatos;

namespace Ent.Inscripcion.Core.Core
{
    [Serializable]
    public class CatalogoItemLista :
      ReadOnlyListBase<CatalogoItemLista, CatalogoItemInfo>
    {
        #region Factory Methods

        public static CatalogoItemLista GetListaCatalogos(Guid catalogoId)
        {
            return DataPortal.Fetch<CatalogoItemLista>(catalogoId);
        }

        public static CatalogoItemLista GetListaCatalogos(string codigo)
        {
            return DataPortal.Fetch<CatalogoItemLista>(codigo);
        }

        private CatalogoItemLista()
        { /* require use of factory methods */ }

        #endregion


        #region Data Access

        private void DataPortal_Fetch(Guid catalogoId)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;
            // TODO: load values
            var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
            List<Parametro> parametros = new List<Parametro>();
            parametros.Add(new Parametro("Id", catalogoId));
            parametros.Add(new Parametro("Codigo", ""));
            var datos = accesoDatos.EjecutarReader("ConsultarItem", parametros);
            while (datos.Datos.Read())
            {
                Add(CatalogoItemInfo.GetListaCatalogosInfo(datos));
            }
            if (!datos.Datos.IsClosed) datos.Datos.Close();
            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        private void DataPortal_Fetch(string codigo)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;
            // TODO: load values
            var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
            var parametros = new List<Parametro> {new Parametro("Id", ""), new Parametro("Codigo", codigo)};
            var datos = accesoDatos.EjecutarReader("ConsultarItem", parametros);
            while (datos.Datos.Read())
            {
                Add(CatalogoItemInfo.GetListaCatalogosInfo(datos));
            }
            if (!datos.Datos.IsClosed) datos.Datos.Close();
            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }
        #endregion
    }


    [Serializable]
    public class CatalogoItemInfo : ReadOnlyBase<CatalogoItemInfo>
    {
        #region Business Methods


        private static PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(o => o.Id, "Name");
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
        }

        private static PropertyInfo<Guid> IdCatalogoProperty = RegisterProperty<Guid>(o => o.IdCatalogo, "Name");
        public Guid IdCatalogo
        {
            get { return GetProperty(IdCatalogoProperty); }
        }

        public static readonly PropertyInfo<string> CodigoProperty = RegisterProperty<string>(c => c.Codigo);
        /// <Summary>
        /// Gets or sets the codigo value.
        /// </Summary>
        public string Codigo
        {
            get { return GetProperty(CodigoProperty); }
        }

        public static readonly PropertyInfo<string> NombreProperty = RegisterProperty<string>(c => c.Nombre);
        /// <Summary>
        /// Gets or sets the nombre value.
        /// </Summary>
        public string Nombre
        {
            get { return GetProperty(NombreProperty); }
        }

        public static readonly PropertyInfo<string> DescripcionProperty = RegisterProperty<string>(c => c.Descripcion);
        /// <Summary>
        /// Gets or sets the Descripcion value.
        /// </Summary>
        public string Descripcion
        {
            get { return GetProperty(DescripcionProperty); }
        }

        #endregion

        #region Factory Methods

        internal static CatalogoItemInfo GetListaCatalogosInfo(Reader childData)
        {
            return DataPortal.FetchChild<CatalogoItemInfo>(childData);
        }

        private CatalogoItemInfo()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void Child_Fetch(Reader childData)
        {
            var id = childData.Datos.GetString(0);
            var idCat = childData.Datos.GetString(1);
            if (id.Length < 5)
                LoadProperty(IdProperty, id);
            else
                LoadProperty(IdProperty, Guid.Parse(id));
            if (idCat.Length < 5)
                LoadProperty(IdCatalogoProperty, idCat);
            else
                LoadProperty(IdCatalogoProperty, Guid.Parse(childData.Datos.GetString(1)));
            LoadProperty(CodigoProperty, childData.Datos.GetString(2));
            LoadProperty(NombreProperty, childData.Datos.GetString(3));
            LoadProperty(DescripcionProperty, childData.Datos.GetString(4));
        }

        #endregion
    }
}
